package loadiconhandler

import (
	"fmt"
	"gmap-backend/internal/config"
	"gmap-backend/internal/server/vo"
	"gmap-backend/internal/util/pathutil"
	"strings"
)

type CpPathLoadIcon struct {
}

func (owner *CpPathLoadIcon) CanHandle(appConfig *config.AppConfig, req *vo.PathReq) (bool, any) {
	urlMatch := strings.HasPrefix(req.Path, "cppath:///") || strings.HasPrefix(req.Path, "cppath://")
	if urlMatch {
		path := strings.TrimSpace(strings.TrimPrefix(strings.TrimPrefix(req.Path, "cppath:///"), "cppath://"))
		return true, path
	}
	return false, nil
}

func (owner *CpPathLoadIcon) Handle(appConfig *config.AppConfig, req *vo.PathReq, extra any) *vo.LoadIconResp {
	path := extra.(string)
	if !pathutil.ParseValidPath(path, req.CtxDir).Succ {
		panic(fmt.Sprintf("无法匹配到有效路径：\r\n%s", path))
	}
	return &vo.LoadIconResp{
		Url: SPECIAL_PATH_CPPATH,
	}
}
